﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;

namespace IRIS.Controllers
{
    public class ContentCategoryController : Controller
    {
        DataRepo rep = new DataRepo();
        // GET: /ContentCategory/List

        [Authorize(Roles = "Stjórnendur")]
        [HttpGet]
        public ActionResult List()
        {
            var cData = rep.getContentCategories();
            return View(cData);
        }

        [Authorize(Roles = "Stjórnendur")]
        [HttpGet]
        public ActionResult Create()
        {
            ContentCategory cData = new ContentCategory();

            return View(cData);
        }

        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Create(ContentCategory theCategory)
        {
            if (string.IsNullOrEmpty(theCategory.Name))
                ModelState.AddModelError("Name", "Nafn á flokki verður að vera til staðar");
            if (string.IsNullOrEmpty(theCategory.Name))
                ModelState.AddModelError("Description", "Lýsing á flokki verður að vera til staðar");
            try
            {
                if (ModelState.IsValid)
                {
                    //Everything is alright and we commit to database 
                    theCategory.Alive = true;
                    theCategory.NewsCat = false;
                    rep.addContentCategory(theCategory);
                    return RedirectToAction("List");
                }
  
                    //Something is wrong and we return the model back with error state
                    return View(theCategory);
                
            }
            catch
            {
                //Something is not o.k so we return error state
                return View(theCategory);
            }
        }
        [Authorize(Roles = "Stjórnendur")]
        [HttpGet]
        public ActionResult Edit(int? id, Boolean delete)
        {
            if (id.HasValue)
            {
                var nData = rep.getContentCategoryById(id.Value);
 
                if (nData != null && delete == true)
                {
                    nData.Alive = false;
                    rep.editContentCategory(id.Value, nData);
                    return RedirectToAction("List");
                }
                
                    return View(nData);
                
            }
            return View("NotFound");
        }

        [Authorize(Roles = "Stjórnendur")]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id, ContentCategory theCategory)
         {
            if (string.IsNullOrEmpty(theCategory.Name))
                ModelState.AddModelError("Name", "Nafn á flokki verður að vera til staðar");
            if (string.IsNullOrEmpty(theCategory.Description))
                ModelState.AddModelError("Description", "Lýsing á flokki verður að vera til staðar");
            try
            {
                if (ModelState.IsValid)
                {
                    //Everything is alright and we commit to database 
                    rep.editContentCategory(id, theCategory);
                    return RedirectToAction("List");
                }
                
                    //Something is wrong and we return the model back with error state
                    return View(theCategory);
            }
            catch
            {
                //Something is not o.k so we return error state
                return View(theCategory);
            }
        }      
    }
}
